package com.ybb.vue.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ybb.vue.common.Result;
import com.ybb.vue.common.util.ExcelUtils;
import com.ybb.vue.domain.GoodsTypeEnum;
import com.ybb.vue.domain.vo.GoodsInventoryExportVo;
import com.ybb.vue.entity.GoodsInventory;
import com.ybb.vue.mapper.GoodsInventoryMapper;
import com.ybb.vue.service.GoodsInventoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ybb
 * @version 1.0
 * @Description  防疫物资清单controller
 * @Date 2023/4/4 18:37
 */

@RestController
@RequestMapping("/goodsInventory")
public class GoodsInventoryController {

    @Autowired
    private GoodsInventoryService goodsInventoryService;

    @Autowired
    private GoodsInventoryMapper goodsInventoryMapper;

    /**
     * 物资清单列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/goodsInventoryList")
    @PreAuthorize("hasAuthority('epidemic:goods:list')"+"||"+"hasAuthority('epidemic:goods:query')")
    public Result<?> goodsInventoryList (@RequestParam(defaultValue = "1") Integer pageNum,
                                           @RequestParam(defaultValue = "10") Integer pageSize,
                                         @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<GoodsInventory> queryWrapper = Wrappers.<GoodsInventory>lambdaQuery();
        queryWrapper.like(GoodsInventory::getGoodsName, search);
        queryWrapper.orderByDesc(GoodsInventory::getCreateTime);
        Page<GoodsInventory> result = goodsInventoryService.page(new Page<>(pageNum, pageSize),queryWrapper);
        List<GoodsInventory> goodsInventoryList = result.getRecords();
        goodsInventoryList.forEach(goodsInventory -> {
            // 物资类别转译
            goodsInventory.setGoodsType(GoodsTypeEnum.getGoodsType(goodsInventory.getGoodsType()));
        });
        return Result.success(result);
    }

    /**
     * 导出
     * @param response
     * @param search
     */
    @GetMapping("/export")
    @PreAuthorize("hasAuthority('epidemic:goods:export')")
    public void exportResident(HttpServletResponse response, @RequestParam(defaultValue = "") String search) {
        List<GoodsInventory> goodsInventoryList = goodsInventoryMapper.selectList(new QueryWrapper<GoodsInventory>().lambda().like(GoodsInventory::getGoodsName, search));
        List<GoodsInventoryExportVo> exportList = new ArrayList<>();
        goodsInventoryList.forEach(goodsInventory -> {
            GoodsInventoryExportVo goodsInventoryExportVo = new GoodsInventoryExportVo();
            BeanUtils.copyProperties(goodsInventory, goodsInventoryExportVo);
            goodsInventoryExportVo.setGoodsType(GoodsTypeEnum.getGoodsType(goodsInventoryExportVo.getGoodsType()));
            exportList.add(goodsInventoryExportVo);
        });
        ExcelUtils.export(response, "入库信息表", exportList, GoodsInventoryExportVo.class);

    }

    /**
     * 物资清单详情
     * @param id
     * @return
     */
    @GetMapping("/byId")
    @PreAuthorize("hasAuthority('epidemic:goods:detail')")
    public Result<?> byId(Long id) {
        GoodsInventory goodsInventory = goodsInventoryMapper.selectById(id);
        goodsInventory.setGoodsType(GoodsTypeEnum.getGoodsType(goodsInventory.getGoodsType()));
        return Result.success(goodsInventory);
    }

    /**
     * 物资清单列表 不分页，添加入库使用
     * @return
     */
    @GetMapping("/goodsInventoryListNoPages")
    @PreAuthorize("hasAuthority('epidemic:goods:in')")
    public Result<?> goodsInventoryListNoPages () {
        LambdaQueryWrapper<GoodsInventory> queryWrapper = Wrappers.<GoodsInventory>lambdaQuery();
        queryWrapper.orderByDesc(GoodsInventory::getCreateTime);
        List<GoodsInventory> goodsInventoryList = goodsInventoryMapper.selectList(queryWrapper);
        goodsInventoryList.forEach(goodsInventory -> {
            // 物资类别转译
            goodsInventory.setGoodsType(GoodsTypeEnum.getGoodsType(goodsInventory.getGoodsType()));
        });
        return Result.success(goodsInventoryList);
    }

}
